'use strict';
app.controller('logisticsCtrl',['$scope','logisticsService','utilService','toaster','$state','$modal',
                            function($scope,logisticsService,utilService,toaster,$state,$modal) {
    $scope.logistics = {};
    // call server.
    $scope.query = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'logistics',
            params: $scope.logistics
        }).then(function (result) {
        });
    }
    $scope.query();


    $scope.updateSort = function(id){
        logisticsService.sort($scope,id).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.reload("app.logistics");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    $scope.openDelete = function (id,name) {
        var modalInstance = $modal.open({
            templateUrl: 'deleteLogistics.html',
            controller: 'deleteLogisticsCtrl',
            scope:$scope,
            size:'sm',
            resolve: {
                data:function(){
                    var data={};
                    data.id=id;
                    data.name=name;
                    return data;
                }
            }
        });

        modalInstance.result.then(function (result) {
            console.log(result);
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });

    };
}]);

app.controller('logisticsAddCtrl',['$scope','logisticsService','$stateParams','toaster','$state',
    function($scope,logisticsService,$stateParams,toaster,$state) {
        $scope.AllowToAdd=true;
        $scope.addLogistics = function(){
            if(!$scope.AllowToAdd){
                return;
            }
            $scope.AllowToAdd=false;
            logisticsService.logisticsAdd($scope).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToAdd=true;
                        $state.go("app.logistics");
                        break;
                    case 604:
                        toaster.pop('error', "操作失败", "行业名称已存在");
                        $scope.AllowToAdd=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToAdd=true;
                        break
                }
            });
        }
}]);


app.controller('logisticsViewCtrl',['$scope','logisticsService','$stateParams','$state','toaster',
    function($scope,logisticsService,$stateParams,$state,toaster) {
        $scope.id = $stateParams.id;
        logisticsService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.logistics = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }

        });
        $scope.AllowToEdit=true;
        $scope.update = function(){
            if(!$scope.AllowToEdit){
                return;
            }
            $scope.AllowToEdit=false;
            logisticsService.updateLogistics($scope).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToEdit=true;
                        $state.go("app.logistics");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToEdit=true;
                        break
                }
            });
        }
}]);


//删除模态框Controller
app.controller('deleteLogisticsCtrl', ['$scope', '$modalInstance','$state','logisticsService','data','toaster',
    function($scope, $modalInstance,$state,logisticsService,data,toaster) {
        $scope.data=data;

        $scope.AllowToDelete=true;

        //确认删除
        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }

            $scope.AllowToDelete=false;

            var id=$scope.data.id;
            logisticsService.delete($scope,id).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $modalInstance.dismiss('success');
                        $state.reload('app.logistics');
                        $scope.AllowToDelete=true;
                        break;
                    case 607:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToDelete=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToDelete=true;
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

    }])
;